package com.chromatic.competition.view;

import android.content.Intent;
import android.os.Bundle;

import androidx.appcompat.app.AppCompatActivity;
import androidx.navigation.NavController;
import androidx.navigation.Navigation;
import androidx.navigation.ui.AppBarConfiguration;
import androidx.navigation.ui.NavigationUI;

import com.chromatic.competition.R;
import com.chromatic.competition.databinding.ActivityMainBinding;
import com.chromatic.competition.utils.UserLoginStatus;
import com.chromatic.competition.view.account.LoginActivity;
import com.google.android.material.bottomnavigation.BottomNavigationView;

import org.json.JSONException;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
//        SharedPreferences sharedPreferences = getSharedPreferences("login_info", MODE_PRIVATE);
//        String userData = "{\"username\":\"TestAccount\",\"password\":\"123456\",\"email\":\"hanyunchen2023@gmail.com\"," +
//                "\"registrationTime\":\"2023-05-16T00:00:00\",\"lastLoginTime\":\"2023-06-19T09:25:45\",\"user_id\":17,\"" +
//                "phone_number\":\"18859972561\",\"admin\":true,\"deactivated\":false}";
//        SharedPreferences.Editor editor = sharedPreferences.edit();
//        editor.putString("user", userData);
//        editor.apply();

        // 检查用户是否已登录或登录是否已过期
        UserLoginStatus accountManager = new UserLoginStatus(this);
        try {
            if (!accountManager.isUserLoggedIn() || accountManager.isLoginExpired()) {
                // 用户未登录或登录已过期，跳转到登录页面
                Intent intent = new Intent(this, LoginActivity.class);
                startActivity(intent);
                finish();
                return;
            }
        } catch (JSONException e) {
            throw new RuntimeException(e);
        }

        ActivityMainBinding binding = ActivityMainBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());
        setupBottomNavigation();
    }

    private void setupBottomNavigation() {
        BottomNavigationView navView = findViewById(R.id.nav_view);
        NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment_activity_main);

        AppBarConfiguration appBarConfiguration = new AppBarConfiguration.Builder(
                R.id.navigation_home, R.id.navigation_dashboard, R.id.navigation_notifications)
                .build();

        NavigationUI.setupWithNavController(navView, navController);

        navView.setOnItemSelectedListener(item -> {
            int itemId = item.getItemId();
            if (itemId == R.id.navigation_home) {
                navController.navigate(R.id.navigation_home);
                return true;
            } else if (itemId == R.id.navigation_dashboard) {
                navController.navigate(R.id.navigation_dashboard);
                return true;
            } else if (itemId == R.id.navigation_notifications) {
                navController.navigate(R.id.navigation_notifications);
                return true;
            } else if (itemId == R.id.navigation_profile) {
                navController.navigate(R.id.navigation_profile);
                return true;
            }
            return false;
        });
    }
}